package com.lohit.dbtest;

import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

public class PatientDetailsActivity extends Activity {

	Boolean status;

	EditText nameET, ageET, bgrpET, numberET, adressET;

	Button next, cancel;
	String name, age, gender, bgrp, number, address;
	RadioGroup type;
	public final Pattern Phone_Number_PATTERN = Pattern.compile("^[0-9]{10}$");
	public final Pattern AGE_PATTERN = Pattern.compile("^([1-9][0-9]?|)$");
	public final Pattern NAME_PATTERN = Pattern.compile("^[A-Za-z ]{3,20}$");

	DbController controller = new DbController(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_patient);

		initialize();

	}

	private void initialize() {

		nameET = (EditText) findViewById(R.id.nameEditText);

		ageET = (EditText) findViewById(R.id.ageEditText);

		bgrpET = (EditText) findViewById(R.id.bgrpEditText);

		numberET = (EditText) findViewById(R.id.numberEditText);

		adressET = (EditText) findViewById(R.id.addressEditText);

		type = (RadioGroup) findViewById(R.id.genderGrp);
		next = (Button) findViewById(R.id.nextButton);

		cancel = (Button) findViewById(R.id.cancelPatientButton);

	}

	public boolean validateInputs(String name, String age, String gender,
			String bgrp, String number, String address) {

		if (name.equals("") || name.length() == 0) {

			Toast.makeText(this, "Please enter Patient's name ",
					Toast.LENGTH_SHORT).show();

			nameET.requestFocus();

			return false;

		}
		if (!checkName(name)) {

			Toast.makeText(this, "Please enter a valid name ",
					Toast.LENGTH_SHORT).show();

			ageET.requestFocus();

			return false;

		}

		if (age.equals("") || age.length() == 0) {

			Toast.makeText(this, "Please enter Patient's age ",
					Toast.LENGTH_SHORT).show();

			ageET.requestFocus();

			return false;

		}
		if (!checkAge(age)) {

			Toast.makeText(this, "Please enter a valid age ",
					Toast.LENGTH_SHORT).show();

			ageET.requestFocus();

			return false;

		}

		if ("".equals(gender)) {

			Toast.makeText(this, "Please select  Patient's gender ",
					Toast.LENGTH_SHORT).show();

			return false;

		}
		if (number.equals("") || age.length() == 0) {

			Toast.makeText(this, "Please enter Patient's phone number ",
					Toast.LENGTH_SHORT).show();

			numberET.requestFocus();

			return false;

		}
		if (!checkNum(number)) {

			Toast.makeText(this, "Please enter a valid phone number ",
					Toast.LENGTH_SHORT).show();

			numberET.requestFocus();

			return false;

		}

		return true;

	}

	private boolean checkAge(String age) {
		// TODO Auto-generated method stub
		return AGE_PATTERN.matcher(age).matches();
	}

	private boolean checkNum(String num) {
		return Phone_Number_PATTERN.matcher(num).matches();
	}

	private boolean checkName(String name) {
		return NAME_PATTERN.matcher(name).matches();
	}

	public void goToAppointment(String name, String age, String gender,
			String bgrp, String number, String address) {

		long id = controller.createPatient(name, age, gender, bgrp, number,
				address);

		if (!(id == -1)) {

			Toast.makeText(this, "Patient Created", Toast.LENGTH_SHORT).show();

			Intent i = new Intent(PatientDetailsActivity.this,
					CreateAppointmentActivity.class);

			i.putExtra("id", id);
			i.putExtra("phone", number);
			startActivity(i);

		}

	}

	@SuppressWarnings("unused")
	public void onNextButtonClick(View arg0) {

		name = nameET.getText().toString().trim();
		age = ageET.getText().toString().trim();
		bgrp = bgrpET.getText().toString().trim();
		number = numberET.getText().toString().trim();
		address = adressET.getText().toString().trim();

		switch (type.getCheckedRadioButtonId()) {

		case R.id.male:

			gender = "male";

			break;

		case R.id.female:

			gender = "female";

			break;

		}

		boolean status = validateInputs(name, age, gender, bgrp, number,
				address);

		if (status) {

			goToAppointment(name, age, gender, bgrp, number, address);

		}

	}

	@SuppressWarnings("unused")
	public void cancelCreatePatientButton(View arg0) {

		// This method cancels the create patient appointment

		nameET.setText("");
		ageET.setText("");
		bgrpET.setText("");
		numberET.setText("");
		adressET.setText("");

		Intent i = new Intent(PatientDetailsActivity.this, Main.class);
		startActivity(i);

	}

}
